package msgpack;

import org.apache.directory.api.util.Strings;
import org.junit.Test;
import org.mortbay.util.ajax.JSON;
import org.msgpack.MessagePack;
import org.msgpack.annotation.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.UUID;
import rsa.HexUtil;

import static org.junit.Assert.assertEquals;

/**
 * MessagePack6Objects
 *
 * @author yhu
 */
public class MessagePack6Object {

    final Logger logger = LoggerFactory.getLogger(MessagePack6Object.class);

    /**
     * MessageData Message Object
     */
    @Message // Annotation
    public static class MessageData {

        // public fields are serialized.
        public String name;
/*        public String uuid;
        public String name;
        public double version;*/
    }

    /**
     * Test MessagePack6Objects
     */
    @Test
    public void testMessagePack6Objects() throws Exception {
        System.out.println(HexUtil.hex2String("736368656D61"));
        System.out.println(Integer.valueOf("00", 16));

        System.out.println(HexUtil.hex2String("6E616D653138"));
        System.out.println(Integer.valueOf("12", 16));

        System.out.println(HexUtil.hex2String("6E616D6538"));
        logger.debug("MessagePack6Objects for Objects");
        String uuid = UUID.randomUUID().toString();
        // INIT OBJ
        MessageData src = new MessageData();
        src.name = "{\"compact\":true,\"schema\":0}";
        //src.uuid = uuid;
        //src.name = "MessagePack6";
        //src.version = 0.6;
        try {
            MessagePack msgPack = new MessagePack();
            // Serialization
            logger.debug("------ Serialization ------");
            //byte[] bytes = msgPack.write(JSON.parse("{\"compact\":true,\"schema\":0}"));
            byte[] bytes = msgPack.write(JSON.parse("{\"name10\":10,\"name11\":11,\"name12\":12,\"name13\":13,\"name14\":14,\"name15\":15,\"name16\":16,\"name17\":17,\"name18\":18,\"name6\":6,\"name7\":7,\"name4\":4,\"name5\":5,"
                + "\"name2\":2,\"name3\":3,\"name1\":1,\"name8\":8,\"name9\":9,\"name10\":10,\"name11\":11,\"name12\":12,\"name10\":10,\"name11\":11,\"name12\":12,\"name13\":13,\"name14\":14,\"name15\":15,\"name16\":16,\"name17\":17,\"name18\":18,\"name6\":6,\"name7\":7,\"name4\":4,\"name5\":5,\"name2\":2,\"name3\":3,\"name1\":1,\"name8\":8,\"name9\":9}"));
            logger.debug("Bytes Array Length: [{}]", bytes.length);
            System.out.println("toHexString==>" + Strings.toHexString(bytes));
            System.out.println("length==>" + Strings.toHexString(bytes).length());
            // Deserialization
            logger.debug("------ Deserialization ------");
            MessageData dst = msgPack.read(bytes, MessageData.class);
            //logger.debug("Check Object for UUID: [{}]", dst.uuid);
            //assertEquals(uuid, dst.uuid);
        } catch (Exception ex) {
            logger.error("MessagePack Serialization And Deserialization error", ex);
        }
    }
}